package cn.aiyuan;

import cn.aiyuan.domain.Student;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyuan
 * @date 2021/8/30
 */
public class MapTrTest {

    public static void main(String[] args) throws Exception {
        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Student student = new Student("aiyuan" + i, i + 1, "ooo" + i);
            students.add(student);
        }
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<Student> streamSource = env.fromCollection(students);
        SingleOutputStreamOperator<Student> sum = streamSource.keyBy(new KeySelector<Student, Integer>() {
            @Override
            public Integer getKey(Student student) throws Exception {
                return student.getAge() % 2;
            }
        }).sum("age");
        sum.print();
        env.execute();
    }
}
